require('../css/addGoods.less');
window.onload = function () {
        // 获取所有dom
        let nameTexts = document.querySelector('.nameText');  //商品名
        let cTexts = document.querySelector('.cText');  //分类input
        let janinps = document.querySelector('.janinp');  //图片添加
        let btnbox = document.querySelector('.btnbox'); //添加按钮
        let upinps = document.querySelector('.upinp');  //点击图片input
        let resimg = document.querySelector('.resimg');  //添加图片div
        let imgs = document.querySelector('.resimg img'); 
    
        // 定义全局常量
        const URL = 'http://139.9.177.51:5000'
    
        // input点击 间接点击 生效图片
        resimg.addEventListener('click', function() {
            upinps.click()
            console.log(666);
        });
        // input改变事件 上传  渲染
        upinps.addEventListener('change', function() {
            let file = upinps.files[0];
            console.log(file);
            http.postfile('http://139.9.177.51:5000/book/upload', 'imgurl', file, function(res) {
                console.log(res);
                // 
                imgs.src = URL + res.imgurl
            })
        })

        // 添加 转换上传
        btnbox.addEventListener('click', function() {
            // 获取值
            let one = String(nameTexts.value);
            let two = String(cTexts.value);
            let tree = String(janinps.value);
            let fore = String(imgs.src);
            // ajax请求
            http.ajax({
                type: 'post', 
                data: {
                    bookName: one,
                    category: two,
                    desc: tree,
                    imgurl: fore
                },
                url: 'http://139.9.177.51:5000/book/add',
                success: function(res) {
                    console.log(res);
                    location.href = './goods-list.html' 
                }
            });
        })
    
}